package cn.e4j.modular.system.controller;

import cn.e4j.config.properties.E4jProperties;
import cn.e4j.core.base.controller.BaseController;
import cn.e4j.core.util.ToolUtil;
import cn.e4j.core.util.ZipUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.ResourceUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

/**
 * 下载
 *
 * @author yuan_rz
 * @date 2019/03/27 21:16
 */
@Controller
@RequestMapping("/download")
@Slf4j
public class DownloadController extends BaseController {

    @Autowired
    private E4jProperties e4jProperties;

    /**
     * 下载图片
     */
    @RequestMapping(method = RequestMethod.GET, path = "/imgs")
    @ResponseBody
    public void imgs(HttpServletRequest request, HttpServletResponse response) throws IOException {

    }

    /**
     * 下载文件
     */
    @RequestMapping(method = RequestMethod.GET, path = "/files")
    @ResponseBody
    public void files(HttpServletRequest request, HttpServletResponse response) throws IOException {
        //文件名
        String filePath = request.getParameter("file");
        String fileName = request.getParameter("fileName");
        //文件流
        response.setContentType("APPLICATION/OCTET-STREAM");
        response.setHeader("Content-Disposition", "attachment; filename=" + fileName);
        OutputStream out = response.getOutputStream();
        //根目录
        String rootPath = ToolUtil.getWebRootPath(null);
        rootPath = rootPath.substring(0, rootPath.lastIndexOf("/"));
        String basePath = null;
        if (ToolUtil.isNotEmpty(request.getParameter("basePath"))) {
            basePath = rootPath + File.separator  + e4jProperties.getFileUploadPath() + File.separator  + request.getParameter("basePath");
        } else {
            basePath = rootPath + File.separator  + e4jProperties.getFileUploadPath();
        }
        //传输
        if (new File(basePath + File.separator  + filePath).exists()) {
            FileInputStream in = null;
            try {
                in = new FileInputStream(basePath + File.separator  + filePath);
                byte[] b = new byte[1024];
                int i = 0;
                while ((i = in.read(b)) > 0) {
                    out.write(b, 0, i);
                }
                out.flush();
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                if (in != null) {
                    in.close();
                }
                if (out != null) {
                    out.close();
                    response.flushBuffer();
                }
            }
        } else {
            out.write("file is not exists!".getBytes("utf-8"));
        }
    }

    /**
     * 下载文件-压缩
     */
    @RequestMapping(method = RequestMethod.GET, path = "/zipFiles")
    @ResponseBody
    public void zipFiles(HttpServletRequest request, HttpServletResponse response) throws IOException {
        //文件列表
        String[] filePathList = request.getParameter("file").split(",");
        String[] fileNameList = request.getParameter("fileName").split(",");
        //文件流
        response.setContentType("APPLICATION/OCTET-STREAM");
        response.setHeader("Content-Disposition", "attachment; filename=" + "zipFiles.zip");
        ZipOutputStream out = new ZipOutputStream(response.getOutputStream());
        //根目录
        String rootPath = ToolUtil.getWebRootPath(null);
        rootPath = rootPath.substring(0, rootPath.lastIndexOf("/"));
        String basePath = null;
        if (ToolUtil.isNotEmpty(request.getParameter("basePath"))) {
            basePath = rootPath + File.separator  + e4jProperties.getFileUploadPath() + File.separator  + request.getParameter("basePath");
        } else {
            basePath = rootPath + File.separator  + e4jProperties.getFileUploadPath();
        }
        //压缩=>传输
        try {
            for (int i=0;i<filePathList.length;i++) {
                File file = new File(basePath + File.separator + filePathList[i]);
                if(file.exists()){
                    ZipUtils.doZip(file,out,"zipFiles",fileNameList[i]);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (out != null) {
                out.close();
                response.flushBuffer();
            }
        }
    }

}
